package com.gage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gage.commons.result.PageInfo;
import com.gage.commons.result.Tree;
import com.gage.commons.utils.StringUtils;
import com.gage.mapper.SysRoleMapper;
import com.gage.mapper.SysRoleResourceMapper;
import com.gage.mapper.SysUserRoleMapper;
import com.gage.model.SysRole;
import com.gage.model.SysRoleResource;
import com.gage.service.IRoleService;

/**
 *
 * Role 表数据服务层接口实现类
 *
 */
@Service
public class RoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements IRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleResourceMapper sysRoleResourceMapper;
    private Logger log = Logger.getLogger(RoleServiceImpl.class);
    public List<SysRole> selectAll() {
        EntityWrapper<SysRole> wrapper = new EntityWrapper<SysRole>();
        wrapper.orderBy("seq");
        return sysRoleMapper.selectList(wrapper);
    }
    
    @Override
    public PageInfo selectDataGrid(PageInfo pageInfo) {
        Page<SysRole> page = new Page<SysRole>(pageInfo.getNowpage(), pageInfo.getSize());
        
        EntityWrapper<SysRole> wrapper = new EntityWrapper<SysRole>();
        wrapper.orderBy(pageInfo.getSort(), pageInfo.getOrder().equalsIgnoreCase("ASC"));
        selectPage(page, wrapper);
        log.error("=================setRows====="+page.getRecords());
        pageInfo.setRows(page.getRecords());
        log.error("=================getRows====="+pageInfo.getRows());
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    public Object selectTree() {
        List<Tree> trees = new ArrayList<Tree>();
        List<SysRole> sysRoles = this.selectAll();
        for (SysRole sysRole : sysRoles) {
            Tree tree = new Tree();
            tree.setId(sysRole.getId());
            tree.setText(sysRole.getName());

            trees.add(tree);
        }
        return trees;
    }

    @Override
    public void updateRoleResource(Long roleId, String resourceIds) {
        // 先删除后添加,有点爆力
        SysRoleResource sysRoleResource = new SysRoleResource();
        sysRoleResource.setRoleId(roleId);
        sysRoleResourceMapper.delete(new EntityWrapper<SysRoleResource>(sysRoleResource));
        
        String[] resourceIdArray = resourceIds.split(",");
        for (String resourceId : resourceIdArray) {
            sysRoleResource = new SysRoleResource();
            sysRoleResource.setRoleId(roleId);
            sysRoleResource.setResourceId(Long.parseLong(resourceId));
            sysRoleResourceMapper.insert(sysRoleResource);
        }
    }

    @Override
    public List<Long> selectResourceIdListByRoleId(Long id) {
        return sysRoleMapper.selectResourceIdListByRoleId(id);
    }
    
    @Override
    public Map<String, Set<String>> selectResourceMapByUserId(Long userId) {
        Map<String, Set<String>> resourceMap = new HashMap<String, Set<String>>();
        List<Long> roleIdList = sysUserRoleMapper.selectRoleIdListByUserId(userId);
        Set<String> urlSet = new HashSet<String>();
        Set<String> roles = new HashSet<String>();
        for (Long roleId : roleIdList) {
            List<Map<Long, String>> resourceList = sysRoleMapper.selectResourceListByRoleId(roleId);
            if (resourceList != null) {
                for (Map<Long, String> map : resourceList) {
                    if (StringUtils.isNotBlank(map.get("url"))) {
                        urlSet.add(map.get("url"));
                    }
                }
            }
            SysRole sysRole = sysRoleMapper.selectById(roleId);
            if (sysRole != null) {
                roles.add(sysRole.getName());
            }
        }
        resourceMap.put("urls", urlSet);
        resourceMap.put("roles", roles);
        return resourceMap;
    }

}